Fix readJsonFile and add error handling#15
Merged
Conversation
To be able to log errors or debug info from files other than the configuration and extension, we need to ensure the `Logger` class has only one instance throughout, and be able to use globally (Singleton design pattern). Added: - Added a line to instantiate `Logger` from within the logger file and export that single instance. Removed: - Removed `export` keyword from `Logger` class. - Removed the `Logger` instantiation from the extension file. - Removed the private `Configuration::logger` property. - Removed the `Configuration::constructor` method's `logger` param. Changed: - Changed the imports to the new lowercased `logger`, ie the exported instance of `Logger` instead of `Logger` itself. - Changed all references to `this.logger` in `Configuration` class to just reference the new single `logger` instance by removing `this`.
Fixes #14 - Split up the reading of the file contents and the parsing of the JSON into 2 separate variables `fileContent` and `jsonContents` for ease in the `readJsonFile` util function. - Added a null coalescing operator to the `jsonContents` variable to ensure the variable is always an empty object if the parsing function from `jsonc-parser` package returns `null`. - Added a `jsonErrors` variable with a default of an empty array, and passed this variable as the optional param in the json `parse` method to collect the parse errors it encounters. We can then use this array to check if there's errors inside. - Added new `constructJsonParseErrorMsg` util function (and added the function call to `readJsonFile`) to loop through the JSON parse error array and construct the error messages since the `jsonc-parser` package doesn't provide a proper error message itself, and only provides error codes and offsets. For each of the parse errors we construct a message that includes: - A formatted error name obtained using the numeric error code - A line number calculated from the error's offset and length - A column number calculated from the error's offset The messages are formatted like so: `Error [error index] - [Error Name] at [filepath]:[line]:[column]` The filepath format of `filepath:line:column` makes it easy to navigate to the specific place in the file for easier debugging. - Added error handling so that it creates a new internal Error with a specific "Failed to parse..." error message. Then log the error stack to the Output Channel, which consists of the specific message, the parse error messages, and the stack trace of where in the extension it occurred.
Because `readJsonFile` is a make or break kinda function, we should notify the user by showing an error popup dialog. - Added vscode's `showErrorMessage` method to tell the user that errors occurred when parsing JSON files, and the extension won't work correctly. It also opens the Output Channel when the button is clicked. - Enabled the error to be thrown. By `throw`ing the error, we allow the extension to fail startup and crash at the point of parse errors. This is to prevent further code that requires the parsed json from the `readJsonFile` function from throwing an unrelated error such as the "Cannot convert undefined or null to object" error, which makes it harder to debug. So if we fail the extension here, then we can easily debug the root cause.
Fixes the ts error from the workflow because I just forgot to commit it. https://github.com/yCodeTech/auto-comment-blocks/actions/runs/19952456507/job/57215155157?pr=15 - Added new `showChannel` method to logger to show the Output Channel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14
The extension crashes on startup with the
Cannot convert undefined or null to objecterror. This is caused by theObject.keysinConfiguration::setLanguageConfigDefinitionsmethod because the config JSON file failed to parse in thereadJsonFileutil function which made the confignull.When the "jsonc-parser" package encounters a problem parsing the JSON via its
parsefunction, it just returnsnullinstead of erroring.The fix is to provide proper error handling, logging and showing a user error dialog when parse errors occur, directly from
readJsonFilefunction. If errors occur, the error will be thrown fromreadJsonFilewhich will fail extension startup and crash, this is to provide easier debugging, instead of waiting for subsequent code that relies on the function from crashing the extension with an unrelated error.If no errors occur but the JSON file still returns as
null, then thereadJsonFilefunction will return an empty object as a fallback.Changes:
Logger Refactor:
Loggerclass export with a singletonloggerexport inlogger.ts, and updates all imports and usages to use this singleton instead of passing aLoggerinstance around. This simplifies the codebase, centralizes logging, and enables multiple files to use the logger on the same instance.Configuration Initialization:
Configurationclass and its instantiation to no longer require a logger parameter, reflecting the new singleton logger usage.Improved JSON File Error Handling:
readJsonFileutility inutils.tsto provide detailed error messages — including file, line, and column — when JSON parsing fails. It also notifies the user via a VS Code error message and offers to open the output channel for more details, then throws an error to halt execution.Utility Function Enhancement:
constructJsonParseErrorMsghelper function to format JSON parse errors for improved debugging and user feedback.